package com.hrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrms.dao.SysRoleMapper;
import com.hrms.pojo.SysDept;
import com.hrms.dao.SysDeptMapper;
import com.hrms.pojo.SysRole;
import com.hrms.service.SysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * 部门表 服务实现类
 *
 * @author LK
 * @since 2021-10-19
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {
	@Autowired(required = false)
	SysDeptMapper sysDeptMapper;

	@Override
	public HashMap<String, Object> select(SysDept a) {
        HashMap<String, Object> map = new HashMap<>();

        //定义分页对象
        Page<SysDept> page = new Page<>(a.getPage(), a.getRow());
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        //编号查询
        queryWrapper.eq(a.getDeptId() != null, "dept_id", a.getDeptId());
        //Page<AttenManage> aList = attenManageMapper.selectByCon(page, a);

        //用户名的模糊查询
        queryWrapper.like(!StringUtils.isEmpty(a.getDeptName()) && a.getDeptName() != null, "dept_name",
                a.getDeptName());
        //查询数据库
        Page<SysDept> aList = sysDeptMapper.selectPage(page, queryWrapper);
        //当前页集合
        map.put("list", aList.getRecords());
        //总页数
        map.put("totalPage", aList.getPages());
        //总条数
        map.put("total", aList.getTotal());
        //当前页
        map.put("curPage", a.getPage());
        if (a.getPage() == 1) {
            //上一页
            map.put("prePage", 1);

        } else {
            map.put("prePage", a.getPage() - 1);

        }
        if (a.getPage() == aList.getPages()) {
            //下一页
            map.put("nextPage", aList.getPages());

        } else {
            map.put("nextPage", a.getPage() + 1);

        }
        //每页显示条数
        map.put("row", a.getRow());
        return map;
	}

	@Override
	public HashMap<String, Object> add(SysDept a) {
        HashMap<String, Object> map = new HashMap<>();

        int num = sysDeptMapper.insert(a);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
	}

	@Override
	public HashMap<String, Object> del(Integer a) {
        HashMap<String, Object> map = new HashMap<>();

        int num = sysDeptMapper.deleteById(a);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
	}

	@Override
	public HashMap<String, Object> update(SysDept a) {
        HashMap<String, Object> map = new HashMap<>();

        int num = sysDeptMapper.updateById(a);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "保存失败");
        }
        return map;
	}
}
